package com.jic.market.impl.rest.discount.processor;

import com.jic.market.common.exception.MarketErrorCodeEnum;
import com.jic.market.common.exception.MarketException;
import com.jic.market.common.util.Money;
import com.jic.market.entity.PromotionLimitRuleEntity;
import com.jic.market.entity.PromotionMerchantProductEntity;
import com.jic.market.enums.DiscountAmountServiceOperateEnum;
import com.jic.market.impl.rest.discount.model.GoodsDetailModel;
import com.jic.market.mapper.PromotionMerchantProductEntityMapper;
import com.jic.market.request.rest.discount.DiscountBaseRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 预售-尾款计算
 * @author yinhaijun
 * @date: 2020/5/6
 */
@Service("BalanceCalculationProcessor")
public class BalanceCalculationProcessor extends AbstractSingleDiscountProcessor {

    @Autowired
    private PromotionMerchantProductEntityMapper promotionMerchantProductEntityMapper;

    @Override
    public SingleResult isCondition(GoodsDetailModel goodsDetailModel, DiscountBaseRequest request, DiscountAmountServiceOperateEnum operate) {
        if(!checkPromotion(request.getThirdUserId(), request.getAdminOperate().getChildId().toString(), goodsDetailModel.getChannelCodes(), goodsDetailModel.getPromotionMemberGradeList())){
            return new SingleResult(false, "预售-尾款计算"+goodsDetailModel.getPromotionCode()+"会员等级校验未通过");
        }
        PromotionMerchantProductEntity promotionMerchantProductEntityQ = new PromotionMerchantProductEntity();
        promotionMerchantProductEntityQ.setPromotionId(Long.parseLong(goodsDetailModel.getPromotionId()));
        promotionMerchantProductEntityQ.setMpCode(goodsDetailModel.getThirdSkuId());
        PromotionMerchantProductEntity promotionMerchantProductEntity = promotionMerchantProductEntityMapper.query(promotionMerchantProductEntityQ);
        PromotionLimitRuleEntity promotionLimitRuleEntity = new PromotionLimitRuleEntity();
        promotionLimitRuleEntity.setChannelStoreLimit(promotionMerchantProductEntity.getChannelStoreLimit());
        promotionLimitRuleEntity.setChannelIndividualLimit(promotionMerchantProductEntity.getChannelIndividualLimit());
        LimitRuleResult limitRuleResult = checkCommonLimitRule(promotionLimitRuleEntity, goodsDetailModel.getPromotionId(), goodsDetailModel, goodsDetailModel.getFrontPromotionTypeEnum(), request.getThirdUserId());
        if(!limitRuleResult.getLimitCheck()){
            return new SingleResult(false, "预售-尾款计算"+goodsDetailModel.getPromotionCode()+"商品"+goodsDetailModel.getThirdSkuId()+"超出活动购买限制");
        }
        // 预售价格
        BigDecimal salePriceWithTax = promotionMerchantProductEntity.getSalePriceWithTax();
        // 订金
        BigDecimal frontMoney = promotionMerchantProductEntity.getFrontMoney();
        // 订金抵扣额
        BigDecimal frontMoneyDeductAmount = promotionMerchantProductEntity.getFrontMoneyDeductAmount();

        if(null == salePriceWithTax || null == frontMoney || null == frontMoneyDeductAmount){
            throw new MarketException(MarketErrorCodeEnum.SYS_ERROR, "预售-尾款计算"+goodsDetailModel.getPromotionCode()+"未查询到对应的预售价格");
        }
        // 与原价进行比较，需要比原价小, 比0大
        if(!(salePriceWithTax.compareTo(goodsDetailModel.getSalePrice()) == -1 && salePriceWithTax.compareTo(BigDecimal.ZERO) == 1)){
            throw new MarketException(MarketErrorCodeEnum.SYS_ERROR, "预售-尾款计算"+goodsDetailModel.getPromotionCode()+"预售价需要比原价小, 比0大");
        }
        if(!(frontMoney.compareTo(goodsDetailModel.getSalePrice()) == -1 && frontMoney.compareTo(BigDecimal.ZERO) == 1)){
            throw new MarketException(MarketErrorCodeEnum.SYS_ERROR, "预售-尾款计算"+goodsDetailModel.getPromotionCode()+"订金需要比原价小, 比0大");
        }
        if(!(frontMoneyDeductAmount.compareTo(goodsDetailModel.getSalePrice()) == -1 && frontMoneyDeductAmount.compareTo(BigDecimal.ZERO) == 1)){
            throw new MarketException(MarketErrorCodeEnum.SYS_ERROR, "预售-尾款计算"+goodsDetailModel.getPromotionCode()+"订金抵扣额需要比原价小, 比0大");
        }
        SingleResult singleResult = new SingleResult(true, "通过");
        singleResult.setObject(promotionMerchantProductEntity);
        return singleResult;
    }

    @Override
    public void queryShoppingCartAfter(SingleResult singleResult, GoodsDetailModel goodsDetailModel, DiscountBaseRequest request){
        querySettlementAfter(singleResult, goodsDetailModel, request);
    }

    @Override
    public void querySettlementAfter(SingleResult singleResult, GoodsDetailModel goodsDetailModel, DiscountBaseRequest request){
        PromotionMerchantProductEntity promotionMerchantProductEntity = (PromotionMerchantProductEntity) singleResult.getObject();
        // 优惠总价 = (原价 - 预售价)*数量
        BigDecimal single = Money.subtract(goodsDetailModel.getSalePrice(), promotionMerchantProductEntity.getSalePriceWithTax());
        BigDecimal promotionDiscountTotalAmount =  Money.multiply(single, new BigDecimal(goodsDetailModel.getSingleValidNum()));
        goodsDetailModel.setPromotionDiscountTotalAmount(promotionDiscountTotalAmount);
        goodsDetailModel.setSalePriceWithTax(promotionMerchantProductEntity.getSalePriceWithTax());
        goodsDetailModel.setFrontMoney(promotionMerchantProductEntity.getFrontMoney());
        goodsDetailModel.setFrontMoneyDeductAmount(promotionMerchantProductEntity.getFrontMoneyDeductAmount());
    }

    @Override
    public void createAfter(GoodsDetailModel goodsDetailModel, Object request, String thirdUserId){
        // 增加一次
        PromotionMerchantProductEntity promotionMerchantProductEntityQ = new PromotionMerchantProductEntity();
        promotionMerchantProductEntityQ.setPromotionId(Long.parseLong(goodsDetailModel.getPromotionId()));
        promotionMerchantProductEntityQ.setMpCode(goodsDetailModel.getThirdSkuId());
        PromotionMerchantProductEntity promotionMerchantProductEntity = promotionMerchantProductEntityMapper.query(promotionMerchantProductEntityQ);
        Long alreadyFrontCount = promotionMerchantProductEntity.getAlreadyFrontCount();
        if(null == alreadyFrontCount){
            promotionMerchantProductEntity.setAlreadyFrontCount(goodsDetailModel.getSingleValidNum());
        }else {
            promotionMerchantProductEntity.setAlreadyFrontCount(promotionMerchantProductEntity.getAlreadyFrontCount() + goodsDetailModel.getSingleValidNum());
        }
        promotionMerchantProductEntityMapper.update(promotionMerchantProductEntity);
    }


}
